package com.zzh.arc;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.zzh.arc.menu.ArcMenu;
import com.zzh.arc.menu.RayMenu;
import com.zzh.dialog.R;
import com.zzh.dialog.databinding.ActArcMenu2Binding;
import com.zzh.dialog.databinding.ActArcMenuBinding;
import com.zzh.lib.core.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zzh on 2024/8/23.
 *
 * @date: 2024/8/23 10:44
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.arc
 */
public class ArcMenuActivity2 extends Activity {

    ActArcMenu2Binding bind;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bind = ActArcMenu2Binding.inflate(getLayoutInflater());
        setContentView(bind.getRoot());

        if (XXPermissions.isGranted(this, Permission.SYSTEM_ALERT_WINDOW)) {
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);

            // Set window size from screen size
            int size = Math.min(dm.widthPixels, dm.heightPixels);
            getWindow().setLayout((int) (size * SCREEN_RATIO), (int) (size * SCREEN_RATIO));

            mPm = getPackageManager();

            mImageList = new ArrayList<>();
            mImageList.add(findViewById(R.id.imageView11));
            mImageList.add(findViewById(R.id.imageView12));
            mImageList.add(findViewById(R.id.imageView13));
            mImageList.add(findViewById(R.id.imageView21));
            mImageList.add(findViewById(R.id.imageView22));
            mImageList.add(findViewById(R.id.imageView23));
            mImageList.add(findViewById(R.id.imageView31));
            mImageList.add(findViewById(R.id.imageView32));
            mImageList.add(findViewById(R.id.imageView33));

            Intent intent = getIntent();
            mPackageNames = intent.getStringArrayListExtra("package_names");
            Log.e("abc", mPackageNames.size() + "");

            for (int i = 0; i < mImageList.size(); ++i) {
                if (mPackageNames.get(i).length() != 0) {
                    try {
                        mImageList.get(i).setImageDrawable(mPm.getApplicationIcon(mPackageNames.get(i)));
                    } catch (PackageManager.NameNotFoundException e) {
                        Log.e("package", "package name not found");
                        mPackageNames.set(i, "");
                    }
                }

                mImageList.get(i).setOnClickListener(new MyOnClickListener(i));

                mImageList.get(i).setOnLongClickListener(new MyOnLongClickListener(i));
            }
        } else {
            XXPermissions.with(this).permission(Permission.SYSTEM_ALERT_WINDOW).request(new OnPermissionCallback() {
                @Override
                public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                    ToastUtils.show("授予权限悬浮窗");
                }
            });
        }


// Get screen size

    }

    public static final int MY_REQUEST_CODE = 12345;
    private static final float SCREEN_RATIO = 0.6f;
    private ArrayList<ImageView> mImageList;
    private ArrayList<String> mPackageNames;
    private PackageManager mPm;


    private class MyOnClickListener implements View.OnClickListener {

        private int mPosition;

        MyOnClickListener(int pos) {
            mPosition = pos;
        }

        @Override
        public void onClick(View v) {
            if (mPackageNames.get(mPosition).length() != 0) {
                Intent intent = mPm.getLaunchIntentForPackage(mPackageNames.get(mPosition));
                finish();
                startActivity(intent);
            } else {
                Intent intent = new Intent(getBaseContext(), Chooser.class);
                intent.putExtra("MESSAGE_position", mPosition);
                startActivityForResult(intent, MY_REQUEST_CODE);
            }
        }
    }

    private class MyOnLongClickListener implements View.OnLongClickListener {

        private int mPosition;

        MyOnLongClickListener(int pos) {
            mPosition = pos;
        }

        @Override
        public boolean onLongClick(View v) {

            // Vibrate device
//            Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
//            vibrator.vibrate(500);

            // Start chooser
            Intent intent = new Intent(getBaseContext(), Chooser.class);
            intent.putExtra("MESSAGE_position", mPosition);
            startActivityForResult(intent, MY_REQUEST_CODE);
            return true;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("MainActivity", "onActivityResult");
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                int position = data.getIntExtra("MESSAGE_position", 0);
                mPackageNames.set(position, data.getStringExtra("MESSAGE_package_name"));
                try {
                    mImageList.get(position).setImageDrawable(mPm.getApplicationIcon(mPackageNames.get(position)));
                } catch (PackageManager.NameNotFoundException e) {
                    Log.e("package", "package name " + mPackageNames.get(position) + " not found");
                    mPackageNames.set(position, "");
                    mImageList.get(position).setImageResource(R.drawable.plussign);
                }
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("MainActivity", "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("MainActivity", "onDestroy");

        // Start service
        Intent intent = new Intent(this, FloatViewService.class);
        intent.putStringArrayListExtra("package_names", mPackageNames);
        startService(intent);
    }

}
